package com.restaurant.application.padtest.couponcancellation;

import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.EntryTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.generation.basicdata.StringMap;
import com.restaurant.application.coupon.Coupon;
import com.restaurant.application.indexes.couponlist.mysql.CouponListBean;
import com.restaurant.application.indexes.couponlist.mysql.CouponListCriteria;
import com.restaurant.application.indexes.couponlist.mysql.CouponListImpl;
import com.restaurant.application.minifacade.data.IdBO;
import com.restaurant.application.padtest.couponcancellation.support.AbstractCouponCancellation;

import java.util.List;
import java.util.Objects;

public class CouponCancellationImpl extends AbstractCouponCancellation {

    @Override
    public View getFullView() {
        PageForm form = newFormShell(new IdBO()).getForm();
        return getSingleton(UIDataFace.class).newGenericForm(unitPosition(), form);
    }

    @Override
    public View getViewLead(ViewSpec spec) {
        return null;
    }


    private FormShell newFormShell(IdBO idBO) {

        FormShell formShell = new FormShell("优惠券核销", 1, 1);

        formShell.source(idBO, DataModes.VALIDATE);

        formShell.at(0, 0, "优惠券号")
                .getEntry(IdBO.FN_ID, idBO.getId())
                .placeHolder("请输入优惠券号")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(true);

        formShell.at(0, 1, "核销")
                .buttonField(getPath(CMD_CANCELLATION), "", true);
        return formShell;
    }

    @Override
    public ViewUpdate cancellation(IdBO cancellation, int[] seq) {
        List<CouponListBean> search = getIndex(CouponListImpl.class).search(new CouponListCriteria().withIdEquals(cancellation.getId()), null, 1);
        if (search.size() > 0) {
            String s = getUnit(Coupon.class, search.get(0).getCouponUid()).confirmUse(getGate().toString());
            if (Objects.equals("success", s)) {
                return returnViewCall(buildViewCall(
                        unitPosition(),
                        "showErrorMessage",
                        null,
                        new StringMap().putData("errorMessage", "核销成功")));
            } else {
                return returnViewCall(buildViewCall(
                        unitPosition(),
                        "showErrorMessage",
                        null,
                        new StringMap().putData("errorMessage", s)));
            }
        }
        return returnViewCall(buildViewCall(
                unitPosition(),
                "showErrorMessage",
                null,
                new StringMap().putData("errorMessage", "该优惠券不在系统内")));
    }
}
